package com.cennavi.minemapdemo.activity.ue;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.activity.BaseActivty;
import com.cennavi.minemapdemo.util.Config;
import com.cennavi.minemapdemo.util.DensityUtil;
import com.cennavi.minemapdemo.widget.CustomButton;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.camera.CameraUpdateFactory;
import com.minedata.minemap.geometry.CoordinateConverter;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;
import com.minedata.minemap.overlay.MarkerLayer;
import com.minedata.minemap.overlay.MarkerLayerOptions;


public class CoordinateTransformActivty extends BaseActivty {

    //地图视图
    private MapView mapView;
    //地图操作类
    private MineMap mineMap;
    private Context mContext;

    private CustomButton btn;

    private MarkerLayer currentlocation = null;
    private MarkerLayerOptions.MarkerItem markerItem = null;

    private LatLng center;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MinemapAccountManager.getInstance(this, Config.MAP_TOK, Config.OFFLINE_TOK, Config.MAP_ID);

        setContentView(R.layout.activity_ue_coordinate_transform);

        mContext = this;
        mapView = findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);

        initMap();
        initView();

    }

    private void initMap() {
        mapView.setApiBaseUrl(Config.API_BASE_URL);
        mapView.setApiUrl(Config.API_URL);
        mapView.setSpriteImageUrl(Config.SPRITE_GLYPHS_URL);
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @SuppressLint("RtlHardcoded")
            @Override
            public void onMapReady(MineMap oMineMap) {
                mineMap = oMineMap;
                mineMap.getUiSettings().setZoomControlsEnabled(false);
                mineMap.getUiSettings().setRotateGesturesEnabled(true);// 是否可以旋转地图
                mineMap.getUiSettings().setTiltGesturesEnabled(true);
                mineMap.getUiSettings().setLogoEnabled(true);
                mineMap.setStyleUrl(Config.BASE_URL + Config.MAP_ID, new MineMap.OnStyleLoadedListener() {
                    @Override
                    public void onStyleLoaded(String s) {
                        mineMap.setTrafficShow(false); //是否展示交通路况信息
                        addImageToMap();
                    }
                });
                //设置指南针是否显示，默认是显示
                mineMap.getUiSettings().setCompassEnabled(true);
                mineMap.getUiSettings().setCompassGravity(Gravity.LEFT);
//                mineMap.getUiSettings().setCompassImage(getResources().getDrawable(R.mipmap.icon_compass));
                mineMap.getUiSettings().setCompassMargins(DensityUtil.dip2px(mContext, 11), DensityUtil.dip2px(mContext, 80),
                        0, 0);
                mineMap.setCameraPosition(
                        new CameraPosition.Builder().target(new LatLng(Config.mapCentrelat,Config.mapCentrelon)).zoom(14)
                                .tilt(0)//设定倾斜角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .build());
                mineMap.setMaxZoomPreference(17);//设置最高展示级别，最高为17级别
                mineMap.setMinZoomPreference(3);//设置最低的展示级别

                center = new LatLng(32.01627, 118.87309);
                updatePoint();
            }
        });

    }

    //加载可能的图片资源
    private void addImageToMap() {

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_gcoding);
        mineMap.addImage(R.mipmap.icon_gcoding + "", bitmap);
    }

    private void initView() {

        btn = findViewById(R.id.btn);
        final ImageView zoomIn = findViewById(R.id.zoomin);
        final ImageView zoomOut = findViewById(R.id.zoomout);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (view.equals(zoomIn)) {

                    zoomIn(view);
                } else if (view.equals(zoomOut)) {

                    zoomOut(view);
                } else if (view.equals(btn)) {

                    perfomCoordinateTransform();
                }
            }
        };

        zoomIn.setOnClickListener(listener);
        zoomOut.setOnClickListener(listener);
        btn.setOnClickListener(listener);

    }

    /**
     *
     */
    private void perfomCoordinateTransform() {

        CoordinateConverter coordinateConverter = new CoordinateConverter();

        if (btn.isClickDown()) {

            coordinateConverter.from(CoordinateConverter.CoordType.WGS84);
            btn.setClickDown(false);
            btn.setText(R.string.toWGS84);
        } else {

            coordinateConverter.from(CoordinateConverter.CoordType.GCJ02);
            btn.setClickDown(true);
            btn.setText(R.string.toGCJ02);
        }
        coordinateConverter.coord(center);
        center = coordinateConverter.convert();

        updatePoint();
    }


    private void updatePoint() {

        if (currentlocation == null || !currentlocation.isLayerExist()) {

            if (markerItem == null) {
                markerItem = new MarkerLayerOptions.MarkerItem().position(center).icon(R.mipmap.icon_gcoding + "");
            }
            MarkerLayerOptions options = new MarkerLayerOptions("currentLocation1");
            options.add(markerItem);
            currentlocation = mineMap.addLayer(options);
        } else {
            markerItem.position(center);
            currentlocation.update();

        }
    }

    public void zoomIn(View view) {
        if (mineMap != null) {
            mineMap.moveCamera(CameraUpdateFactory.zoomIn());
        }
    }

    public void zoomOut(View view) {
        if (mineMap != null) {
            mineMap.moveCamera(CameraUpdateFactory.zoomOut());
        }
    }


    //地图生命周期
    @Override
    protected void onStart() {
        super.onStart();
        if (mapView != null)
            mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mapView != null)
            mapView.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mapView != null)
            mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mapView != null)
            mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapView != null)
            mapView.onDestroy();
    }
}
